<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useCartStore } from '../store/cart' // 导入购物车 store

const cartStore = useCartStore() // 使用购物车 store

// 购物车商品列表现在从 Pinia store 获取
const cartItems = computed(() => cartStore.items)

// 选中的商品ID列表
const selectedItems = ref<number[]>([])

// 全选状态
const isAllSelected = computed(() => {
  return cartItems.value.length > 0 && selectedItems.value.length === cartItems.value.length
})

// 选中商品的总价
const totalPrice = computed(() => {
  return cartItems.value
    .filter(item => selectedItems.value.includes(item.id))
    .reduce((total, item) => total + item.price * item.quantity, 0)
})

// 选中商品的数量
const totalQuantity = computed(() => {
  return cartItems.value
    .filter(item => selectedItems.value.includes(item.id))
    .reduce((total, item) => total + item.quantity, 0)
})

// 切换全选状态
const toggleSelectAll = () => {
  if (isAllSelected.value) {
    selectedItems.value = []
  } else {
    selectedItems.value = cartItems.value.map(item => item.id)
  }
}

// 切换单个商品选中状态
const toggleSelectItem = (id: number) => {
  const index = selectedItems.value.indexOf(id)
  if (index === -1) {
    selectedItems.value.push(id)
  } else {
    selectedItems.value.splice(index, 1)
  }
}

// 更新商品数量
const updateQuantity = (id: number, delta: number) => {
  const item = cartItems.value.find(item => item.id === id)
  if (item) {
    if (delta === 1) {
      cartStore.addItem(item) // 增加数量
    } else if (delta === -1) {
      cartStore.removeItem(id) // 减少数量
    }
  }
}

// 删除商品
const removeItem = (id: number) => {
  cartStore.removeItem(id) // 使用 store 的 removeItem
  const selectedIndex = selectedItems.value.indexOf(id)
  if (selectedIndex !== -1) {
    selectedItems.value.splice(selectedIndex, 1)
  }
}

// 结算方法
const checkout = () => {
  if (selectedItems.value.length === 0) {
    alert('请选择要结算的商品')
    return
  }
  // TODO: 实现结算逻辑
  console.log('结算选中的商品', selectedItems.value)
  alert(`结算 ${totalQuantity.value} 件商品，总价 ¥${totalPrice.value}`);
  // 结算后清空购物车中选中的商品
  selectedItems.value.forEach(id => {
    const item = cartItems.value.find(item => item.id === id);
    if (item) {
      // 假设结算后从购物车中移除，这里需要根据实际业务逻辑调整
      // 如果是完全移除，可以调用 cartStore.removeItem(id)
      // 如果只是减少数量，则需要更复杂的逻辑
    }
  });
  // 简单起见，这里假设结算后清空整个购物车
  cartStore.clearCart();
  selectedItems.value = []; // 清空选中项
}

onMounted(() => {
  // 为购物车项目设置入场动画延迟
  const items = document.querySelectorAll('.cart-item');
  items.forEach((item, index) => {
    (item as HTMLElement).style.animationDelay = `${index * 0.1 + 0.5}s`; // 0.5s 是标题动画后的延迟
  });
});
</script>

<template>
  <div class="cart-page-wrapper">
    <div class="cart-container">
      <div class="cart-title-header">
        <h2 class="cart-main-title">
          <span>购</span><span>物</span><span>车</span>
        </h2>
        <p class="cart-subtitle">管理您的精选商品</p>
      </div>

      <!-- 购物车列表 -->
      <div class="cart-list" v-if="cartItems.length > 0">
        <!-- 表头 -->
        <div class="cart-header">
          <label class="checkbox-wrapper custom-checkbox">
            <input
              type="checkbox"
              :checked="isAllSelected"
              @change="toggleSelectAll"
            >
            <span class="checkmark"></span>
            <span class="select-all-text">全选</span>
          </label>
          <span class="header-item product-info-header">商品信息</span>
          <span class="header-item">单价</span>
          <span class="header-item">数量</span>
          <span class="header-item">小计</span>
          <span class="header-item">操作</span>
        </div>

        <!-- 商品列表 -->
        <div v-for="item in cartItems" :key="item.id" class="cart-item">
          <label class="checkbox-wrapper custom-checkbox">
            <input
              type="checkbox"
              :checked="selectedItems.includes(item.id)"
              @change="toggleSelectItem(item.id)"
            >
            <span class="checkmark"></span>
          </label>
          <div class="item-info">
            <div class="item-image-wrapper">
              <img :src="item.cover" :alt="item.title"> <!-- 使用 item.cover -->
            </div>
            <div class="item-details">
              <h3>{{ item.title }}</h3>
              <p class="item-type">{{ item.duration }}</p> <!-- 使用 item.duration -->
              <!-- <p class="item-date">使用日期：{{ item.date }}</p> -->
            </div>
          </div>
          <div class="item-price">¥{{ item.price }}</div>
          <div class="item-quantity">
            <button @click="updateQuantity(item.id, -1)" class="quantity-btn">-</button>
            <span class="quantity-display">{{ item.quantity }}</span>
            <button @click="updateQuantity(item.id, 1)" class="quantity-btn">+</button>
          </div>
          <div class="item-subtotal">¥{{ item.price * item.quantity }}</div>
          <button class="remove-btn" @click="removeItem(item.id)">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash3-fill" viewBox="0 0 16 16">
              <path d="M11 1.5v1h3.5a.5.5 0 0 1 0 1h-.538l-.853 10.66A2 2 0 0 1 11.115 16h-6.23a2 2 0 0 1-1.994-1.84L2.038 3.5H1.5a.5.5 0 0 1 0-1H5v-1A1.5 1.5 0 0 1 6.5 0h3A1.5 1.5 0 0 1 11 1.5m-5 0v1h4v-1a.5.5 0 0 0-.5-.5h-3a.5.5 0 0 0-.5.5M4.5 5.029l.5 8.5a.5.5 0 1 0 .998-.06l-.5-8.5a.5.5 0 1 0-.998.06m6.53-.528a.5.5 0 0 0-.528.47l-.5 8.5a.5.5 0 0 0 .998.058l.5-8.5a.5.5 0 0 0-.47-.528M8 4.5a.5.5 0 0 0-.5.5v8.5a.5.5 0 0 0 1 0V5a.5.5 0 0 0-.5-.5"/>
            </svg>
            <span>删除</span>
          </button>
        </div>
      </div>

      <!-- 空购物车提示 -->
      <div v-else class="empty-cart">
        <div class="empty-cart-icon">🛒</div>
        <p>购物车空空如也</p>
        <span>快去挑选心仪的商品吧！</span>
        <button class="go-shopping-btn">去逛逛</button>
      </div>

      <!-- 结算栏 -->
      <div class="checkout-bar" v-if="cartItems.length > 0">
        <div class="checkout-selection-info">
          <label class="checkbox-wrapper custom-checkbox">
            <input
              type="checkbox"
              :checked="isAllSelected"
              @change="toggleSelectAll"
            >
            <span class="checkmark"></span>
            <span class="select-all-text">全选</span>
          </label>
          <span class="selected-count">已选 <span>{{ totalQuantity }}</span> 件商品</span>
        </div>
        <div class="checkout-total-info">
          <span class="total-price-label">总计：</span>
          <span class="total-price">¥{{ totalPrice }}</span>
          <button class="checkout-btn" @click="checkout">去结算</button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 页面整体布局 */
.cart-page-wrapper {
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px); /* 减去导航栏高度 */
  padding: 30px 0;
  font-family: 'Arial', sans-serif;
  color: #333;
}

.cart-container {
  max-width: 1200px;
  margin: 0 auto;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
  padding: 30px;
}

/* 标题部分 */
.cart-title-header {
  text-align: center;
  margin-bottom: 40px;
  overflow: hidden;
}

.cart-main-title {
  font-size: 3.2em;
  font-weight: bold;
  color: #2c3e50;
  margin-bottom: 10px;
  display: inline-block;
}

.cart-main-title span {
  display: inline-block;
  opacity: 0;
  transform: translateY(20px);
  animation: slideInUp 0.8s forwards ease-out;
}

@keyframes slideInUp {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.cart-subtitle {
  font-size: 1.1em;
  color: #7f8c8d;
}

/* 购物车列表 */
.cart-list {
  margin-bottom: 30px;
}

.cart-header {
  display: grid;
  grid-template-columns: 50px 3fr 1fr 1.5fr 1fr 1fr;
  gap: 15px;
  align-items: center;
  background-color: #ecf0f1;
  padding: 15px 20px;
  border-radius: 8px;
  font-weight: bold;
  color: #34495e;
  margin-bottom: 15px;
}

.header-item {
  text-align: center;
}

.product-info-header {
  text-align: left;
  padding-left: 10px;
}

.cart-item {
  display: grid;
  grid-template-columns: 50px 3fr 1fr 1.5fr 1fr 1fr;
  gap: 15px;
  align-items: center;
  background-color: #ffffff;
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
  transition: all 0.3s ease;
  border-radius: 8px;
  margin-bottom: 10px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.03);
  opacity: 0;
  transform: translateY(20px);
  animation: fadeInItem 0.6s forwards ease-out;
}

@keyframes fadeInItem {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.cart-item:hover {
  box-shadow: 0 6px 15px rgba(0, 0, 0, 0.07);
  transform: translateY(-3px);
}

.item-info {
  display: flex;
  align-items: center;
  text-align: left;
}

.item-image-wrapper {
  width: 80px;
  height: 80px;
  border-radius: 8px;
  overflow: hidden;
  margin-right: 15px;
  flex-shrink: 0;
}

.item-image-wrapper img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.item-details h3 {
  font-size: 1.1em;
  margin: 0 0 5px 0;
  color: #2c3e50;
}

.item-type,
.item-date {
  font-size: 0.9em;
  color: #7f8c8d;
  margin-bottom: 3px;
}

.item-price,
.item-subtotal {
  font-size: 1.1em;
  font-weight: bold;
  color: #e74c3c;
  text-align: center;
}

.item-quantity {
  display: flex;
  align-items: center;
  justify-content: center;
}

.quantity-btn {
  background-color: #3498db;
  color: white;
  border: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-size: 1.2em;
  cursor: pointer;
  transition: background-color 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.quantity-btn:hover {
  background-color: #2980b9;
}

.quantity-display {
  margin: 0 10px;
  font-size: 1.1em;
  font-weight: bold;
  color: #34495e;
}

.remove-btn {
  background: none;
  border: none;
  color: #e74c3c;
  cursor: pointer;
  font-size: 0.9em;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  transition: color 0.3s ease;
}

.remove-btn:hover {
  color: #c0392b;
}

/* 自定义复选框 */
.checkbox-wrapper {
  display: block;
  position: relative;
  padding-left: 30px;
  cursor: pointer;
  font-size: 1em;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  text-align: center;
}

.checkbox-wrapper input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 20px;
  width: 20px;
  background-color: #eee;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.checkbox-wrapper:hover input ~ .checkmark {
  background-color: #ccc;
}

.checkbox-wrapper input:checked ~ .checkmark {
  background-color: #3498db;
}

.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

.checkbox-wrapper input:checked ~ .checkmark:after {
  display: block;
}

.checkbox-wrapper .checkmark:after {
  left: 7px;
  top: 3px;
  width: 6px;
  height: 12px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}

.select-all-text {
  margin-left: 5px;
  font-weight: bold;
}

/* 空购物车 */
.empty-cart {
  text-align: center;
  padding: 50px 20px;
  background-color: #fdfdfd;
  border-radius: 12px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
}

.empty-cart-icon {
  font-size: 6em;
  margin-bottom: 20px;
  color: #bdc3c7;
}

.empty-cart p {
  font-size: 1.5em;
  color: #555;
  margin-bottom: 10px;
}

.empty-cart span {
  font-size: 1.1em;
  color: #7f8c8d;
  display: block;
  margin-bottom: 30px;
}

.go-shopping-btn {
  background-color: #3498db;
  color: white;
  border: none;
  padding: 12px 30px;
  border-radius: 30px;
  font-size: 1.1em;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.3s ease;
}

.go-shopping-btn:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
}

/* 结算栏 */
.checkout-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #ffffff;
  padding: 20px 30px;
  border-radius: 12px;
  box-shadow: 0 -5px 15px rgba(0, 0, 0, 0.05);
  margin-top: 20px;
}

.checkout-selection-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.selected-count {
  font-size: 1.1em;
  color: #555;
}

.selected-count span {
  color: #3498db;
  font-weight: bold;
}

.checkout-total-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.total-price-label {
  font-size: 1.1em;
  color: #555;
}

.total-price {
  font-size: 1.8em;
  font-weight: bold;
  color: #e74c3c;
}

.checkout-btn {
  background-color: #2ecc71;
  color: white;
  border: none;
  padding: 12px 30px;
  border-radius: 30px;
  font-size: 1.1em;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.3s ease;
}

.checkout-btn:hover {
  background-color: #27ae60;
  transform: translateY(-2px);
}

/* 响应式设计 */
@media (max-width: 992px) {
  .cart-header,
  .cart-item {
    grid-template-columns: 50px 2.5fr 1fr 1.5fr 1fr 1fr;
    gap: 10px;
  }

  .item-image-wrapper {
    width: 60px;
    height: 60px;
  }

  .item-details h3 {
    font-size: 1em;
  }

  .item-price,
  .item-subtotal {
    font-size: 1em;
  }

  .quantity-btn {
    width: 25px;
    height: 25px;
    font-size: 1em;
  }

  .quantity-display {
    margin: 0 8px;
    font-size: 1em;
  }

  .remove-btn {
    font-size: 0.8em;
  }

  .checkout-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }

  .checkout-total-info {
    width: 100%;
    justify-content: space-between;
  }
}

@media (max-width: 768px) {
  .cart-container {
    padding: 20px;
  }

  .cart-main-title {
    font-size: 2.5em;
  }

  .cart-header,
  .cart-item {
    grid-template-columns: 40px 2fr 1fr 1.2fr 1fr 0.8fr;
    gap: 8px;
    padding: 12px 15px;
  }

  .item-image-wrapper {
    width: 50px;
    height: 50px;
  }

  .item-details h3 {
    font-size: 0.9em;
  }

  .item-type,
  .item-date {
    font-size: 0.8em;
  }

  .item-price,
  .item-subtotal {
    font-size: 0.9em;
  }

  .quantity-btn {
    width: 22px;
    height: 22px;
    font-size: 0.9em;
  }

  .quantity-display {
    margin: 0 5px;
    font-size: 0.9em;
  }

  .remove-btn {
    font-size: 0.7em;
  }

  .select-all-text {
    display: none;
  }
}

@media (max-width: 480px) {
  .cart-container {
    padding: 15px;
  }

  .cart-main-title {
    font-size: 2em;
  }

  .cart-header,
  .cart-item {
    grid-template-columns: 30px 1.5fr 0.8fr 1fr 0.8fr 0.6fr;
    gap: 5px;
    padding: 10px;
  }

  .item-image-wrapper {
    width: 40px;
    height: 40px;
  }

  .item-details h3 {
    font-size: 0.8em;
  }

  .item-price,
  .item-subtotal {
    font-size: 0.8em;
  }

  .quantity-btn {
    width: 20px;
    height: 20px;
    font-size: 0.8em;
  }

  .quantity-display {
    margin: 0 3px;
    font-size: 0.8em;
  }

  .remove-btn {
    font-size: 0.6em;
  }

  .checkout-bar {
    padding: 15px 20px;
  }

  .total-price {
    font-size: 1.5em;
  }

  .checkout-btn {
    padding: 10px 20px;
    font-size: 1em;
  }
}
</style>